package edu.xhu.video.aliyun;

import com.aliyun.green20220302.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.Getter;

/**
 * 双剑枷锁之单例client
 */
public class AliyunClient {
    private static volatile Client client;

    @Getter
    public static final String accessKeyId = "LTAI5tKJSf7kPuq2j4ns15uN";
    @Getter
    public static final String secret = "Oy90HzzW7VZpoPEIOG9P8A07R3xb16";

    private AliyunClient() {

    }

    public static synchronized Client getInstance() {
        if (client == null) {
            synchronized (AliyunClient.class) {
                if (client == null) {
                    try {
                        Config config = new Config();
                        config.setAccessKeyId(accessKeyId);
                        config.setAccessKeySecret(secret);
                        config.setRegionId("cn-shanghai");
                        config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
                        config.setReadTimeout(6000);
                        config.setConnectTimeout(3000);
                        client = new Client(config);
                    } catch (Exception e) {
                        throw new RuntimeException();
                    }
                }
            }
        }
        return client;
    }

    //双剑枷锁之销毁对象
    public static synchronized boolean destoryInstance() {
        if (client != null) {
            synchronized (AliyunClient.class) {
                if (client != null) {
                    client = null;
                    return true;
                }
            }
        }
        return false;
    }


}
